<?php

class M_obat extends CI_Model{
    
    public function getAllObat(){
        $strSql = "SELECT * FROM tb_obat";
        $strQuery = $this->db->query($strSql);
        if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $val) {
                $arrHasil[] = $val;
            }
            $strQuery->free_result();
            return $arrHasil;
        }else{
            return array();
        }
    }
    
    
    public function getAllObatWithLimit($pArrParam){
        $strSql = "SELECT * FROM tb_obat LIMIT ?,?";
        $strQuery = $this->db->query($strSql, $pArrParam);
        if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $val) {
                $arrHasil[] = $val;
            }
            $strQuery->free_result();
            return $arrHasil;
        }else{
            return array();
        }
    }
    
    public function getObat($pIntObatId){
        $strSql = "SELECT * FROM tb_obat WHERE id_obat=?";
        $strQuery = $this->db->query($strSql, $pIntObatId);
        if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $val) {
                $arrHasil[] = $val;
            }
            $strQuery->free_result();
            return $arrHasil;
        }else{
            return array();
        } 
    }
    
    public function saveAdd($pArrParams){
        $boolStatus = FALSE;
        
        //cek duplikasi id
        $this->db->where('id_jenis',$pArrParams[0]);
        $this->db->where('nama',$pArrParams[1]);
        $this->db->from("tb_obat");
        $count = $this->db->count_all_results();
        
        if($count > 0){
            $boolStatus = FALSE;
        }else{
            $strSql = "INSERT INTO tb_obat(id_jenis,nama,dosis,aturan,stok,harga) VALUES (?,?,?,?,?,?)";
            if($this->db->query($strSql, $pArrParams)){
                $boolStatus = TRUE;
            }else{
                $boolStatus = FALSE;
            }
        }
        return $boolStatus;
    }
    
    public function saveEdit($pArrParams){
        $strSql = "UPDATE tb_obat SET id_jenis=?, nama=?, dosis=?, aturan=?, stok=?, harga=? WHERE id_obat=?";
        return $this->db->query($strSql,$pArrParams);
    }
    
    function delete($pStrObatId) {
        $strSql = "DELETE FROM tb_obat WHERE id_obat=?";
        return  $this->db->query($strSql,$pStrObatId);
    }
    
    function count() {
        return $this->db->count_all_results("tb_obat");
    }
    
}

?>
